//调用阿里云短信sdk
const SMSClient = require('@alicloud/sms-sdk');
function sendSMS(phoneNumber, signName, templateCode,smsCode) {
  let accessKeyId = 'LTAI5tJsnU39FYpg3PnVB6Jd'   //AccessKey Id
  let secretAccessKey = 'YafeMNA3YbZHGmud2NojXRupTiRGfQ'; // AccessKey Secret
 
  // 初始化sms_client
  const smsClient = new SMSClient({ accessKeyId, secretAccessKey });

  // 开始发送短信
  return smsClient.sendSMS({
    PhoneNumbers: phoneNumber,
    SignName: signName,
    TemplateCode: templateCode,
    TemplateParam: `{"code":${smsCode}}` // 短信模板变量对应的实际值，JSON格式
  }).then(result => {
    console.log("result：", result);
    let { Code } = result;
    if (Code == 'OK') {
      return {
        code: 0,
        msg: 'success',
        sms: smsCode
      };
    }
  }).catch(err => {
    console.log("报错：", err);
    return {
      code: 1,
      msg: 'fail: ' + err.data.Message
    };
  });
}

module.exports = {
  sendSMS
};